package com.bupt.secret;


import com.alibaba.fastjson2.JSONObject;
import com.bupt.secret.constant.AlgorithmConstants;
import com.bupt.secret.dto.*;
import com.bupt.secret.utils.BuptClientUtil;
import com.bupt.secret.utils.BuptResultDto;

import java.util.Arrays;


/**
 * @author ShiShuMin
 * @version v1.0
 * @ClassName BuptSecretApplication
 * @Date: 2024/8/24 11:54
 * @Description:
 */
public class BuptSecretApplication {

    public static void main(String[] args){

        BuptResultDto buptResultDto = BuptClientUtil.openSession();
        Object data = buptResultDto.getData();
        Long session = (Long)data;

//        DeviceInfo deviceInfo = BuptClientUtil.getDeviceInfo(session);
//        System.out.println("deviceinfo：" + JSONObject.toJSONString(deviceInfo));
//
////        BuptClientUtil.generateKeyWithIPKRSA(session, 1, 128);
//
        // 获取随机数
//        BuptResultDto ramdom = BuptClientUtil.generateRandom(session, 4);
//        System.out.println("ramdom:"+ JSONObject.toJSONString(ramdom));
//
//        BuptResultDto<RSArefPublicKey> encPublicKeyRSA = BuptClientUtil.exportEncPublicKeyRSA(session, 1);
//        System.out.println("encPublicKeyRSA:" + JSONObject.toJSONString(encPublicKeyRSA));
////        {"autoRead":true,"autoWrite":true,"bits":2048,"e":"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAQ==","m":"0tvY2o7DsYkMLiWEkWRU4au4XKGiVMTT2SG9B2yeWPl1p0wpasZyRvjrX9gYpe4PeBzPSvhEvcq991hdsuXXEyJCr7d5ZOiJIo4j1Zsx8VDTIMeqOiIYnVeCg1p/rNCP50gFOrqEu9L4lw6dohTHbmAE1fHeSM8QskLSF60jqCIQs+pc3h4k43C92WjWqvZEepxjXr3k/hc2OEGyFLL+BmY1DxVqXsVsWv4Jl89wnqmf96T4DLPGhQPJGegbVcJjV/UkFVQj4qgRKee+6oNYr0sJmN55rKSFYTY65ZEWP/p8N2jyBcIwBscdD1fpV0XYgcCARI3P1KsIfedmpjA/jQ==","pointer":{}}
//
//        BuptResultDto<RSArefCommonKeyBo> commonKey = BuptClientUtil.generateKeyPairRSA(session, 2048);
//        System.out.println("commonKey:" + JSONObject.toJSONString(commonKey));


        BuptResultDto<RSArefCommonKeyBo> rsArefCommonKeyBoBuptResultDto = BuptClientUtil.generateKeyPairRSAV2(session, 2048,"123321", 2049);
        System.out.println("生成RSA密钥对,私钥用口令加密输出:"+JSONObject.toJSONString(rsArefCommonKeyBoBuptResultDto));
        BuptClientUtil.closeSession(session);


        BuptResultDto buptResultDto2 = BuptClientUtil.openSession();
        session = (Long)buptResultDto2.getData();
        RSArefPublicKey rsArefPublicKey = rsArefCommonKeyBoBuptResultDto.getData().getRsArefPublicKey();
        CipherRSArefPrivateKey rsArefPrivateKey = rsArefCommonKeyBoBuptResultDto.getData().getRsArefPrivateKey();
        BuptResultDto status = BuptClientUtil.importKeyPairRSAV2(session, 10,"123321", 2049, rsArefPublicKey, rsArefPrivateKey);
        System.out.println("\n导入RSA密钥对,私钥已做加密处理:"+JSONObject.toJSONString(status));

////
        BuptResultDto<RSASessionKeyBo> rsaSessionKeyBoBuptResultDto = BuptClientUtil.generateKeyWithEPKRSA(session, 16, rsArefPublicKey);
        System.out.println("\n生成会话密钥并用外部RSA公钥加密输出:" + JSONObject.toJSONString(rsaSessionKeyBoBuptResultDto));
        RSASessionKeyBo rsaSessionKeyBo = rsaSessionKeyBoBuptResultDto.getData();
        BuptClientUtil.closeSession(session);

        BuptResultDto buptResultDto3 = BuptClientUtil.openSession();
        session = (Long)buptResultDto3.getData();
        BuptResultDto resultDto = BuptClientUtil.importKeyWithISKRSA(session, 10, rsaSessionKeyBo.getPucKey(), rsaSessionKeyBo.getPuiKeyLength());
        System.out.println("导入会话密钥并用内部RSA私钥解密:"+JSONObject.toJSONString(resultDto));
        Long pk = (Long) resultDto.getData();


//        RSASessionKeyBo rsaSessionKeyBo = rsaSessionKeyBoBuptResultDto.getData();
        long phKeyHandle = rsaSessionKeyBoBuptResultDto.getData().getPhKeyHandle();
        byte[] iv = "1234567812345678".getBytes();

        String[] testData = {"12345678", "123131dkdfeeirnr", "{\"dfdfsaf1231231231221432\":1231}", "{\"对吧123\":\"?2123是打发第三方$#$@??\"}"};

        for (String testStr : testData) {
            String plain = testStr;
            System.out.println("\n--------- Test Plan --------");
            System.out.println("原始字符        : "+ plain);
            System.out.println("原始字符长度     : "+ plain.length());

            BuptResultDto<EncDataBo> encrypt = BuptClientUtil.encrypt(session, phKeyHandle, AlgorithmConstants.SGD_AES_CBC, iv, plain);
            EncDataBo encryptDataBo = encrypt.getData();
            System.out.println("加密后         : "+ Arrays.toString(encryptDataBo.getPucEncData()));
            System.out.println("加密长度       : "+ encryptDataBo.getPuiEncDataLength());
            System.out.println("加密IV       : "+ JSONObject.toJSONString(encrypt.getData().getPucIV()));

            BuptResultDto<DecDataBo> decrypt =  BuptClientUtil.decrypt(session, pk, AlgorithmConstants.SGD_AES_CBC, encrypt.getData().getPucIV(),encryptDataBo.getPucEncData(), encryptDataBo.getPuiEncDataLength());
            DecDataBo decryptDataBo = decrypt.getData();
            System.out.println("解密后         : "+ Arrays.toString(decryptDataBo.getPucData()));
            System.out.println("解密后长度      : "+ decryptDataBo.getPuiDataLength());

            // 转换为字符串
            String ivString = new String(decryptDataBo.getPucData());
            System.out.println("解密还原文本     : "+ ivString);
            System.out.println("解密还原文本长度  : "+ ivString.length());

            System.out.println("解密数据和原始数据: " + (plain.equals(ivString)? "一致" : "不一致"));

        }

        BuptClientUtil.closeSession(session);
    }


}
